/*
 * Interface de remoção de turma.
 */
package amanda_gabriel_isabella_lucivania_monica.View;

import amanda_gabriel_isabella_lucivania_monica.Model.Dao.DaoBaseImpl;
import amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Professor;
import amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Turma;
import java.util.List;
import java.util.Objects;
import java.util.Vector;
import javax.swing.DefaultListModel;

/**
 *
 * @author lucy
 */
public class RemoverTurmaIG extends javax.swing.JFrame {

    private DaoBaseImpl daoBaseImpl;

    /**
     * Cria um novo form RemoverTurma
     *
     * @param daoBaseImpl
     */
    public RemoverTurmaIG(DaoBaseImpl daoBaseImpl) {
        initComponents();
        this.daoBaseImpl = daoBaseImpl;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jDSucesso = new javax.swing.JDialog();
        jLRemovidoSucesso = new javax.swing.JLabel();
        jLRealizarRemocao = new javax.swing.JLabel();
        jBSim = new javax.swing.JButton();
        jBNao = new javax.swing.JButton();
        jDCamposVazios = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jBOK = new javax.swing.JButton();
        amanda_gabriel_isabella_lucivania_monicaPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("amanda_gabriel_isabella_lucivania_monicaPU").createEntityManager();
        turmaQuery = java.beans.Beans.isDesignTime() ? null : amanda_gabriel_isabella_lucivania_monicaPUEntityManager.createQuery("SELECT t FROM Turma t");
        turmaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : turmaQuery.getResultList();
        jPPainelListarTurmaRemover = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLListarTurmaRemover = new javax.swing.JList();
        jBRemover = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();

        jDSucesso.setTitle("Sucesso");
        jDSucesso.setMinimumSize(new java.awt.Dimension(400, 175));

        jLRemovidoSucesso.setText("Turma removida com sucesso!");

        jLRealizarRemocao.setText("Deseja realizar outra remoção?");

        jBSim.setText("Sim");
        jBSim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSimActionPerformed(evt);
            }
        });

        jBNao.setText("Não");
        jBNao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNaoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDSucessoLayout = new javax.swing.GroupLayout(jDSucesso.getContentPane());
        jDSucesso.getContentPane().setLayout(jDSucessoLayout);
        jDSucessoLayout.setHorizontalGroup(
            jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDSucessoLayout.createSequentialGroup()
                .addGap(69, 69, 69)
                .addComponent(jBSim, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57)
                .addComponent(jBNao)
                .addContainerGap(52, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDSucessoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLRealizarRemocao)
                    .addComponent(jLRemovidoSucesso))
                .addGap(41, 41, 41))
        );

        jDSucessoLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBNao, jBSim});

        jDSucessoLayout.setVerticalGroup(
            jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDSucessoLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLRemovidoSucesso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLRealizarRemocao)
                .addGap(24, 24, 24)
                .addGroup(jDSucessoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSim)
                    .addComponent(jBNao))
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jDCamposVazios.setMinimumSize(new java.awt.Dimension(400, 175));

        jLabel1.setText("Todos os campos devem ser preenchidos!");

        jBOK.setText("OK");
        jBOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDCamposVaziosLayout = new javax.swing.GroupLayout(jDCamposVazios.getContentPane());
        jDCamposVazios.getContentPane().setLayout(jDCamposVaziosLayout);
        jDCamposVaziosLayout.setHorizontalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGroup(jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(69, 69, 69)
                        .addComponent(jLabel1))
                    .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                        .addGap(148, 148, 148)
                        .addComponent(jBOK, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(27, Short.MAX_VALUE))
        );
        jDCamposVaziosLayout.setVerticalGroup(
            jDCamposVaziosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDCamposVaziosLayout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jBOK)
                .addContainerGap(47, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Remover turma");

        jPPainelListarTurmaRemover.setBorder(javax.swing.BorderFactory.createTitledBorder("Por favor, selecione a turma a qual deseja remover:"));

        org.jdesktop.swingbinding.JListBinding jListBinding = org.jdesktop.swingbinding.SwingBindings.createJListBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, turmaList, jLListarTurmaRemover);
        bindingGroup.addBinding(jListBinding);

        jScrollPane1.setViewportView(jLListarTurmaRemover);

        javax.swing.GroupLayout jPPainelListarTurmaRemoverLayout = new javax.swing.GroupLayout(jPPainelListarTurmaRemover);
        jPPainelListarTurmaRemover.setLayout(jPPainelListarTurmaRemoverLayout);
        jPPainelListarTurmaRemoverLayout.setHorizontalGroup(
            jPPainelListarTurmaRemoverLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPPainelListarTurmaRemoverLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(43, 43, 43))
        );
        jPPainelListarTurmaRemoverLayout.setVerticalGroup(
            jPPainelListarTurmaRemoverLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPPainelListarTurmaRemoverLayout.createSequentialGroup()
                .addContainerGap(67, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );

        jBRemover.setText("Remover");
        jBRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoverActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBRemover)
                        .addGap(18, 18, 18)
                        .addComponent(jBCancelar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPPainelListarTurmaRemover, javax.swing.GroupLayout.PREFERRED_SIZE, 487, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPPainelListarTurmaRemover, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBCancelar)
                    .addComponent(jBRemover))
                .addGap(21, 21, 21))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jBCancelarActionPerformed

    private void jBSimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSimActionPerformed
        setVisible(true);
        jDSucesso.dispose();
    }//GEN-LAST:event_jBSimActionPerformed

    private void jBRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoverActionPerformed
        //EntidadeTurma turma = (EntidadeTurma) jLListarTurmaRemover.getSelectedValue();
        int index = jLListarTurmaRemover.getSelectedIndex();

        if (index == -1) {
            jDCamposVazios.setVisible(true);
            setVisible(false);
        } else {
            Object objeto = jLListarTurmaRemover.getModel().getElementAt(index);
            daoBaseImpl.remove(objeto);
            jLListarTurmaRemover.setSelectedIndex(-1);
            jDSucesso.setVisible(true);
        }
        
        jLListarTurmaRemover.setModel(new DefaultListModel());
        
        List<Objects> objetos = daoBaseImpl.executarQuery("SELECT p FROM Turma p",null,null);
        Vector objetos2 = new Vector();
        for(Object objeto:objetos){
            objetos2.addElement(objeto);
        }
        jLListarTurmaRemover.setListData(objetos2);
        jLListarTurmaRemover.setVisible(true);
        
        
    }//GEN-LAST:event_jBRemoverActionPerformed

    private void jBNaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNaoActionPerformed
        jDSucesso.dispose();
        setVisible(false);
    }//GEN-LAST:event_jBNaoActionPerformed

    private void jBOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBOKActionPerformed
        setVisible(true);
        jDCamposVazios.dispose();
    }//GEN-LAST:event_jBOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager amanda_gabriel_isabella_lucivania_monicaPUEntityManager;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBNao;
    private javax.swing.JButton jBOK;
    private javax.swing.JButton jBRemover;
    private javax.swing.JButton jBSim;
    private javax.swing.JDialog jDCamposVazios;
    private javax.swing.JDialog jDSucesso;
    private javax.swing.JList jLListarTurmaRemover;
    private javax.swing.JLabel jLRealizarRemocao;
    private javax.swing.JLabel jLRemovidoSucesso;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPPainelListarTurmaRemover;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<amanda_gabriel_isabella_lucivania_monica.Model.Pojo.Turma> turmaList;
    private javax.persistence.Query turmaQuery;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
